<?php
require_once "../config/dbConfig.php";
require_once "../utils/response.php";

/**
 * 创建文章
 * @param array $data 文章数据
 */
function createArticle($data) {
    global $conn;
    
    try {
        // 处理图片路径，现在直接从前端传递
        $imgPath = '';
        if (isset($data['img']) && !empty($data['img'])) {
            $imgPath = $data['img'];
        }

        // 准备SQL语句
        $sql = "INSERT INTO mental_content (title, content, img, source, brief, create_user, update_user, create_time, update_time, type_id) 
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        
        $stmt = $conn->prepare($sql);
        
        // 绑定参数
        $stmt->bind_param("sssssiissi", 
            $data['title'],
            $data['content'],
            $imgPath,
            $data['source'],
            $data['brief'],
            $data['create_user'],
            $data['create_user'], // update_user 同 create_user
            $data['create_time'],
            $data['update_time'],
            $data['type_id']
        );
        
        // 执行SQL
        if ($stmt->execute()) {
            Response::success('文章创建成功', ['id' => $conn->insert_id])->send();
        } else {
            Response::error('文章创建失败：' . $stmt->error)->send();
        }
        
        $stmt->close();
        
    } catch (Exception $e) {
        Response::error('文章创建失败：' . $e->getMessage())->send();
    }
}

/**
 * 更新文章
 * @param array $data 文章数据
 */
function updateArticle($data) {
    global $conn;
    
    try {
        // 使用条件判断来处理有无图片的情况
        if (isset($data['img']) && !empty($data['img'])) {
            // 有图片的情况
            $sql = "UPDATE mental_content 
                    SET title = ?, content = ?, source = ?, brief = ?, 
                        update_user = ?, update_time = ?, type_id = ?, img = ?
                    WHERE id = ?";
                    
            $stmt = $conn->prepare($sql);
            
            $stmt->bind_param("ssssisssi", 
                $data['title'],
                $data['content'],
                $data['source'],
                $data['brief'],
                $data['create_user'], // 更新者ID
                $data['update_time'],
                $data['type_id'],
                $data['img'],
                $data['id']
            );
        } else {
            // 无图片的情况
            $sql = "UPDATE mental_content 
                    SET title = ?, content = ?, source = ?, brief = ?, 
                        update_user = ?, update_time = ?, type_id = ?
                    WHERE id = ?";
                    
            $stmt = $conn->prepare($sql);
            
            $stmt->bind_param("ssssisii", 
                $data['title'],
                $data['content'],
                $data['source'],
                $data['brief'],
                $data['create_user'], // 更新者ID
                $data['update_time'],
                $data['type_id'],
                $data['id']
            );
        }
        
        // 执行SQL
        if ($stmt->execute()) {
            Response::success('文章更新成功')->send();
        } else {
            Response::error('文章更新失败：' . $stmt->error)->send();
        }
        
        $stmt->close();
        
    } catch (Exception $e) {
        Response::error('文章更新失败：' . $e->getMessage())->send();
    }
}

/**
 * 通过文章id删除文章
 * @param $id
 * @return void
 */
function deleteArticleById($id)
{
    global $conn;
    
    try {
        // 准备SQL语句
        $sql = "DELETE FROM mental_content WHERE id = ?";
        
        $stmt = $conn->prepare($sql);
        
        // 绑定参数
        $stmt->bind_param("i", $id);
        
        // 执行SQL
        if ($stmt->execute()) {
            Response::success('文章删除成功')->send();
        } else {
            Response::error('文章删除失败：' . $stmt->error)->send();
        }
        
        $stmt->close();
        
    } catch (Exception $e) {
        Response::error('文章删除失败：' . $e->getMessage())->send();
    }
}